<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/1
 * Time: 10:36
 */

namespace app\admin\controller;

//提现申请
use app\Model\RebateLogModel;
use app\Model\RemittanceModel;
use app\Model\UsersModel;
use app\Model\WithdrawalsModel;

class Withdrawals extends Common
{
    /*
     * 提现申请页
     */
    public function index(){
        $postData = input('get.');
        $where = [];
        $time=[0,time()];
        if (isset($postData['status'])){
            if ($postData['status']!=-1){//收货人
                $where['status'] = ['=', $postData['status']];
            }
            if ($postData['user_id']!=''){//用户id
                $where['user_id'] = ['=', $postData['user_id']];
            }
            if ($postData['account_bank']!=''){//收款账号
                $where['account_bank'] = ['LIKE', '%'.$postData['account_bank'].'%'];
            }
            if ($postData['account_name']!=''){//收款账户名
                $where['account_name'] = ['LIKE', '%'.$postData['account_name'].'%'];
            }
            if ($postData['create_time']){//申请时间
                $postData['create_time']=explode(" - ", $postData['create_time']);//时间分割成[0]第一个 [1]第二个 取中间值
                $time[0]=strtotime($postData['create_time'][0]);
                $time[1]=strtotime($postData['create_time'][1])+86399;
            }

        }
        $getWithdrawals= WithdrawalsModel::
        order('status','asc')//提现状态0申请中1申请成功2申请失败
            ->where($where)//筛选
            ->whereTime('create_time', 'between', [$time[0], $time[1]])//筛选时间
            ->paginate(null,false,['query'=>request()->param()]);
        $data['data'] = $getWithdrawals;
        $this->getConAndAct('member');

        return view('index',$data);
    }
    /*
     * 编辑提现申请页
     */
    public function add()
    {
        $getData=input('get.');
        $data['withdrawals'] = WithdrawalsModel::alias('w')
            ->join('tp_users u','w.user_id=u.user_id')//用户表
            ->find($getData['id']);
//        dump($data['withdrawals']->toArray());
        return view('add',$data);
    }
    /*
     * 拒绝提现
     */
    public function  refuse()
    {
        $getData= input('post.');
        $setWit=WithdrawalsModel::update(['id'=>$getData['id'],'status'=>2]);//拒绝提现
        return setDatabase($setWit,'拒绝提现');
    }

    /*
     * 提现  生成转账记录
     */
    public function addData()
    {
        $getData = input('post.');

        $update = ['id'=>$getData['id'],'remark'=>$getData['remark'],'status'=>1];
        $setWit= WithdrawalsModel::update($update);//改变提现状态
        setDatabaseDef($setWit,'提现状态没有改变');

        $getWit = WithdrawalsModel::find($getData['id']);

        //扣除用户余额
        $setUser=UsersModel::where('user_id','=',$getData['user_id'])->setDec('user_money',$getWit->commission);
        setDatabaseDef($setUser,'用户佣金扣除'.$getWit->commission.'失败');
        //                $User->where('id=5')->setDec('score',5); // 用户的积分减5

        //生成转账记录
        $insert=[
            'user_id'=>$getData['user_id'],//用户id
            'bank_name'=>$getWit->bank_name,//银行名称
            'account_bank'=>$getWit->account_bank,//银行账户
            'account_name'=>$getWit->account_name,//开户人名称
            'money'=>$getWit->money,//汇款金额
            'create_time'=>time(),//汇款时间
            'remark'=>$getWit->remark,//汇款备注
            'admin_id'=>session('admin.id'),//管理员id
            'withdrawals_id'=>$getWit->id,//提现申请表id
            'status'=>1,//状态
        ];
        $addRem=RemittanceModel::insert($insert);
        setDatabaseDef($addRem,'转账记录生成失败');
        return retJson('',200,'提现成功');


    }
    /*
     * 删除提现申请
     */
    public function del(){
        $postData = input('post.');
        $delRole = WithdrawalsModel::destroy($postData['id']);
        return setDatabase($delRole);
    }


    /*
     * 汇款记录/转账记录 列表
     */
    public function transfer()
    {
        $postData = input('get.');
        $where = [];
        $time=[0,time()];
        if (isset($postData['status'])){
            if ($postData['status']!=-1){//收货人
                $where['status'] = ['=', $postData['status']];
            }
            if ($postData['user_id']!=''){//用户id
                $where['user_id'] = ['=', $postData['user_id']];
            }
            if ($postData['account_bank']!=''){//收款账号
                $where['account_bank'] = ['LIKE', '%'.$postData['account_bank'].'%'];
            }
            if ($postData['account_name']!=''){//收款账户名
                $where['account_name'] = ['LIKE', '%'.$postData['account_name'].'%'];
            }
            if ($postData['create_time']){//汇款时间
                $postData['create_time']=explode(" - ", $postData['create_time']);//时间分割成[0]第一个 [1]第二个 取中间值
                $time[0]=strtotime($postData['create_time'][0]);
                $time[1]=strtotime($postData['create_time'][1])+86399;
            }

        }
        $getWithdrawals= RemittanceModel::
        order('create_time','desc')
        ->where($where)//筛选
        ->whereTime('create_time', 'between', [$time[0], $time[1]])//筛选时间
        ->paginate(null,false,['query'=>request()->param()]);
        $data['data'] = $getWithdrawals;
        $this->getConAndAct('member');
        return view('transferindex',$data);
    }

    /*
      * 佣金明细
      */
    public function commission_log(){
        $postData = input('get.');
        $time=[0,time()];
        $where=[];
        if (isset($postData['status'])){
            if ($postData['status']!=-1){//收货人
                $where['status'] = ['=', $postData['status']];
            }
            if ($postData['user_id']!=''){//用户id
                $where['user_id'] = ['=', $postData['user_id']];
            }
            if ($postData['account_bank']!=''){//收款账号
                $where['account_bank'] = ['LIKE', '%'.$postData['account_bank'].'%'];
            }
            if ($postData['account_name']!=''){//收款账户名
                $where['account_name'] = ['LIKE', '%'.$postData['account_name'].'%'];
            }
            if ($postData['create_time']){//申请时间
                $postData['create_time']=explode(" - ", $postData['create_time']);//时间分割成[0]第一个 [1]第二个 取中间值
                $time[0]=strtotime($postData['create_time'][0]);
                $time[1]=strtotime($postData['create_time'][1])+86399;
            }

        }
        $getWithdrawals= RebateLogModel::alias('r')->field('r.*')
            ->join('tp_users u','u.user_id =r.user_id')
            ->field('u.mobile,u.nickname as u_nickname')
            ->order('r.create_time','desc')//提现状态0申请中1申请成功2申请失败
            ->where($where)//筛选
            ->whereTime('create_time', 'between', [$time[0], $time[1]])//筛选时间
            ->paginate(null,false,['query'=>request()->param()]);
//        dump($getWithdrawals->toArray());die();
        $data['data'] = $getWithdrawals;
        $this->getConAndAct('member');

        return view('commission_log',$data);
    }
}